package com.example.attack.config;

import com.example.attack.statemachine.Events;
import com.example.attack.statemachine.States;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.action.Actions;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.configurers.StateConfigurer;
import org.springframework.statemachine.guard.Guard;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

import java.util.EnumSet;

@Configuration
@EnableStateMachine
public class StateMachineConfig
        extends EnumStateMachineConfigurerAdapter<States, Events> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config)
            throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(listener());
    }

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states)
            throws Exception {
        states
                .withStates()
                .initial(States.S1)
//                .state(States.S3, actionS2_1(), actionS2_2())
//                .state(States.S4, actionS2_1(), actionS2_2())
//                .state(States.S5, actionS2_1(), actionS2_2())
                .states(EnumSet.allOf(States.class))
                .fork(States.S2)
                .join(States.S5);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(States.S1).target(States.S2).event(Events.E1).action(Actions.errorCallingAction(actionS2_1(), actionS2_2()))
                .and()
                .withFork().source(States.S2).target(States.S3).target(States.S4)
                .and()
                .withJoin().source(States.S3).source(States.S4).target(States.S5)
                .and()
                .withExternal()
                .source(States.S5).target(States.S6);
    }

    @Bean
    public StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>() {
            @Override
            public void stateChanged(State<States, Events> from, State<States, Events> to) {
                System.out.println("State change to " + to.getId());
            }
        };
    }
    
    @Bean
    public Action<States, Events> actionS2_1(){
        return stateContext -> {
//            throw new RuntimeException("lzj");
            System.out.println(stateContext.getTransition().getTarget().getId());
        };
    }

    @Bean
    public Action<States, Events> actionS2_2(){
        return stateContext -> {
            System.out.println("2");
            System.out.println(stateContext.getException().getMessage());
        };
    }

    @Bean
    public Guard<States, Events> s2Guard() {
        return new Guard<States, Events>() {

            @Override
            public boolean evaluate(StateContext<States, Events> context) {
                return true;
            }
        };
    }
}
